package com.txunda.zhuohired.aty.user.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.tools.AlertDialog;
import com.ants.theantsgo.util.JSONUtils;
import com.ants.theantsgo.util.L;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.aty.user.find.ShareAty;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.home.JobDetailsBean;
import com.txunda.zhuohired.dialog.ApplyDialog;
import com.txunda.zhuohired.http.pst.MemberHomePst;

import java.util.Map;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/5 14:41
 * 功能描述：兼职详情页
 * 联系方式：jiahuanchen@126.com
 */

public class PartJobDetailsAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_job_details_center)
    private TextView tv_layout_job_details_center;
    /**
     * 收藏
     */
    @ViewInject(R.id.iv_layout_job_details_collect)
    private ImageView iv_layout_job_details_collect;
    /**
     * 结算方式
     */
    @ViewInject(R.id.iv_part_details_type)
    private ImageView iv_part_details_type;
    /**
     * 职位
     */
    @ViewInject(R.id.tv_part_details_name)
    private TextView tv_part_details_name;
    /**
     * 待遇
     */
    @ViewInject(R.id.tv_part_details_price)
    private TextView tv_part_details_price;
    /**
     * 申请人数
     */
    @ViewInject(R.id.ll_part_details_person)
    private LinearLayout ll_part_details_person;
    @ViewInject(R.id.tv_part_details_person)
    private TextView tv_part_details_person;
    /**
     * 工作地址
     */
    @ViewInject(R.id.ll_part_job_details_address)
    private LinearLayout ll_part_job_details_address;
    @ViewInject(R.id.tv_part_details_address)
    private TextView tv_part_details_address;
    /**
     * 距离
     */
    @ViewInject(R.id.tv_part_details_distance)
    private TextView tv_part_details_distance;
    /**
     * 工作时间
     */
    @ViewInject(R.id.ll_part_details_time)
    private LinearLayout ll_part_details_time;
    @ViewInject(R.id.ll_part_details_work_time)
    private LinearLayout ll_part_details_work_time;
    @ViewInject(R.id.tv_part_details_time)
    private TextView tv_part_details_time;
    /**
     * 工作日期
     */
    @ViewInject(R.id.ll_part_details_date)
    private LinearLayout ll_part_details_date;
    @ViewInject(R.id.tv_part_details_date)
    private TextView tv_part_details_date;
    /**
     * 工作要求
     */
    @ViewInject(R.id.ll_part_details_ask)
    private LinearLayout ll_part_details_ask;
    @ViewInject(R.id.tv_part_details_ask)
    private TextView tv_part_details_ask;
    /**
     * 工作职责
     */
    @ViewInject(R.id.ll_part_details_duty)
    private LinearLayout ll_part_details_duty;
    @ViewInject(R.id.tv_part_details_duty)
    private TextView tv_part_details_duty;
    /**
     * 公司logo
     */
    @ViewInject(R.id.iv_part_details_logo)
    private ImageView iv_part_details_logo;
    /**
     * 公司名称
     */
    @ViewInject(R.id.tv_job_details_company_name)
    private TextView tv_job_details_company_name;
    /**
     * 行业
     */
    @ViewInject(R.id.tv_part_details_industry)
    private TextView tv_part_details_industry;
    /**
     * 规模
     */
    @ViewInject(R.id.tv_part_details_scale)
    private TextView tv_part_details_scale;
    /**
     * 学历
     */
    @ViewInject(R.id.tv_part_details_education)
    private TextView tv_part_details_education;
    @ViewInject(R.id.tv_part_details_education_sure)
    private TextView tv_part_details_education_sure;
    /**
     * 工作经验
     */
    @ViewInject(R.id.tv_part_details_suffer)
    private TextView tv_part_details_suffer;
    @ViewInject(R.id.tv_part_details_suffer_sure)
    private TextView tv_part_details_suffer_sure;
    /**
     * 申请面试
     */
    @ViewInject(R.id.tv_part_job_details_apply)
    private TextView tv_part_job_details_apply;
    /**
     * 立即沟通
     */
    @ViewInject(R.id.tv_job_details_ask)
    private TextView tv_job_details_ask;

    private int collectStatus = 1;
    private String part_details_id = "";
    private String token = "";
    private MemberHomePst memberHomePst;
    private String type = "2";//1为全职 2为兼职
    private String role = "2";//身份 1为求职者 2为公司招人
    private String lat = "39.140800165768";//纬度
    private String lng = "117.21768025899";//经度
    private String apply_status = "";
    private Bundle bundle;
    private String u_id = "";
    private int job_school = 1;
    private int job_experience = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_job_details_head);
        tv_layout_job_details_center.setText("职位详情");
        iv_layout_job_details_collect.setVisibility(View.GONE);
        //        initCollect();
    }

    @Override
    @OnClick({R.id.iv_layout_job_details_share, R.id.ll_part_job_details_address,
            R.id.ll_part_job_details_company, R.id.tv_part_job_details_apply})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_layout_job_details_share://分享
                startActivity(ShareAty.class, null);
                break;
            //            case R.id.iv_layout_job_details_collect://收藏
            //                if (collectStatus == 1) {
            //                    collectStatus = 2;
            //                    initCollect();
            //
            //                } else if (collectStatus == 2) {
            //                    collectStatus = 1;
            //                    initCollect();
            //
            //                }
            //                break;
            case R.id.ll_part_job_details_address://地址
                startActivity(CompanyAddressAty.class, null);
                break;
            case R.id.ll_part_job_details_company://公司详情
                bundle = new Bundle();
                bundle.putString("u_id", u_id);
                startActivity(CompanyDetailsAty.class, bundle);
                break;
            case R.id.tv_part_job_details_apply://申请面试
                if ("0".equals(apply_status)) {
                    if (job_experience == 2 || job_school == 2) {
                        ApplyDialog applyDialog = new ApplyDialog(this, new ApplyDialog.ApplyListener() {
                            @Override
                            public void onApply(String type) {
                                //                            startActivity(ApplySureAty.class, null);
                                memberHomePst.applyJob(token, "2", u_id, part_details_id);
                            }
                        });
                        applyDialog.show();
                    } else {
                        onShowDialog();
                    }
                } else if ("1".equals(apply_status)) {
                    showErrorTip("您已申请面试");
                }
                break;
        }
    }

    private void onShowDialog() {
        new AlertDialog(this).builder().setTitle("提示").setMsg("确认申请面试?")
                .setPositiveButton("确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        //                        L.e("======id======",id);
                        //                        L.e("======model======",model);
                        memberHomePst.applyJob(token, "2", u_id, part_details_id);
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        }).show();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_part_job_details;
    }

    @Override
    protected void initialized() {
        part_details_id = getIntent().getStringExtra("part_details_id");
        token = application.getUserInfo().get("data");
        memberHomePst = new MemberHomePst(this);
    }

    private void initCollect() {
        if (collectStatus == 1) {
            iv_layout_job_details_collect.setImageResource(R.mipmap.icon_job_details_no_collect);
        } else {
            iv_layout_job_details_collect.setImageResource(R.mipmap.icon_job_details_collect);
        }
    }

    @Override
    protected void requestData() {
        initHttp();
    }

    private void initHttp() {
        memberHomePst.reruitInfo(token, type, part_details_id, lat, lng, role);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("reruitInfo")) {
            //            L.e("====================", jsonStr);
            JobDetailsBean jobDetailsBean = GsonUtil.GsonToBean(jsonStr, JobDetailsBean.class);
            //            L.e("==========", jobDetailsBean.toString());
            if ("1".equals(jobDetailsBean.getData().getPart_status())) {
                iv_part_details_type.setVisibility(View.VISIBLE);
                iv_part_details_type.setImageResource(R.mipmap.icon_part_day);
            } else if ("2".equals(jobDetailsBean.getData().getPart_status())) {
                iv_part_details_type.setImageResource(R.mipmap.icon_part_month);
                iv_part_details_type.setVisibility(View.VISIBLE);
            } else {
                iv_part_details_type.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(jobDetailsBean.getData().getTitle())) {
                tv_part_details_name.setVisibility(View.GONE);
            } else {
                tv_part_details_name.setVisibility(View.VISIBLE);
                tv_part_details_name.setText(jobDetailsBean.getData().getTitle());
            }
            tv_part_details_price.setText(jobDetailsBean.getData().getPrice());
            if (TextUtils.isEmpty(jobDetailsBean.getData().getCount())) {
                ll_part_details_person.setVisibility(View.GONE);
            } else {
                ll_part_details_person.setVisibility(View.VISIBLE);
                tv_part_details_person.setText(jobDetailsBean.getData().getCount());
            }
            if (TextUtils.isEmpty(jobDetailsBean.getData().getWork_address()) && TextUtils.isEmpty
                    (jobDetailsBean
                            .getData().getDistance())) {
                ll_part_job_details_address.setVisibility(View.GONE);
            } else {
                ll_part_job_details_address.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(jobDetailsBean.getData().getAddress())) {
                    tv_part_details_address.setVisibility(View.GONE);
                } else {
                    tv_part_details_address.setVisibility(View.VISIBLE);
                    tv_part_details_address.setText(jobDetailsBean.getData().getAddress());
                }
                if (TextUtils.isEmpty(jobDetailsBean.getData().getDistance())) {
                    tv_part_details_distance.setVisibility(View.GONE);
                } else {
                    tv_part_details_distance.setText("距您:" + jobDetailsBean.getData().getDistance());
                    tv_part_details_distance.setVisibility(View.VISIBLE);
                }
            }
            if (TextUtils.isEmpty(jobDetailsBean.getData().getPart_datestart()) && TextUtils.isEmpty
                    (jobDetailsBean.getData().getPart_dateend()) && TextUtils.isEmpty(jobDetailsBean.getData()
                    .getWork_time())) {
                ll_part_details_time.setVisibility(View.GONE);
            } else {
                ll_part_details_time.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(jobDetailsBean.getData().getPart_datestart()) && TextUtils.isEmpty
                        (jobDetailsBean.getData().getPart_dateend())) {
                    ll_part_details_date.setVisibility(View.GONE);
                } else {
                    ll_part_details_date.setVisibility(View.VISIBLE);
                    tv_part_details_date.setText(jobDetailsBean.getData().getPart_datestart() + "-" +
                            jobDetailsBean
                                    .getData().getPart_dateend());
                }
                if (TextUtils.isEmpty(jobDetailsBean.getData().getWork_time())) {
                    ll_part_details_work_time.setVisibility(View.GONE);
                } else {
                    ll_part_details_work_time.setVisibility(View.VISIBLE);
                    tv_part_details_time.setText(jobDetailsBean.getData().getWork_time());
                }
            }
            if (TextUtils.isEmpty(jobDetailsBean.getData().getJob_claim())) {
                ll_part_details_ask.setVisibility(View.GONE);
                tv_part_details_ask.setVisibility(View.GONE);
            } else {
                ll_part_details_ask.setVisibility(View.VISIBLE);
                tv_part_details_ask.setVisibility(View.VISIBLE);
                tv_part_details_ask.setText(jobDetailsBean.getData().getJob_claim());
            }
            if (TextUtils.isEmpty(jobDetailsBean.getData().getJob_duty())) {
                ll_part_details_duty.setVisibility(View.GONE);
                tv_part_details_duty.setVisibility(View.GONE);
            } else {
                ll_part_details_duty.setVisibility(View.VISIBLE);
                tv_part_details_duty.setVisibility(View.VISIBLE);
                tv_part_details_duty.setText(jobDetailsBean.getData().getJob_duty());
            }
            Glide.with(this).load(jobDetailsBean.getData().getLogo()).error(R.mipmap.icon_defult)
                    .placeholder(R
                            .mipmap.icon_defult).dontAnimate().into(iv_part_details_logo);
            if (TextUtils.isEmpty(jobDetailsBean.getData().getName())) {
                tv_job_details_company_name.setVisibility(View.INVISIBLE);
            } else {
                tv_job_details_company_name.setVisibility(View.VISIBLE);
                tv_job_details_company_name.setText(jobDetailsBean.getData().getName());
            }
            if (TextUtils.isEmpty(jobDetailsBean.getData().getCompany_trade())) {
                tv_part_details_industry.setVisibility(View.GONE);
            } else {
                tv_part_details_industry.setVisibility(View.VISIBLE);
                tv_part_details_industry.setText("行业：" + jobDetailsBean.getData().getCompany_trade());
            }
            if (TextUtils.isEmpty(jobDetailsBean.getData().getCompany_scale())) {
                tv_part_details_scale.setVisibility(View.GONE);
            } else {
                tv_part_details_scale.setVisibility(View.VISIBLE);
                tv_part_details_scale.setText("规模：" + jobDetailsBean.getData().getCompany_scale());
            }
            if (TextUtils.isEmpty(jobDetailsBean.getData().getName_shooling())) {
                tv_part_details_education.setVisibility(View.GONE);
            } else {
                tv_part_details_education.setVisibility(View.VISIBLE);
                tv_part_details_education.setText(jobDetailsBean.getData().getName_shooling());
            }
            job_school = jobDetailsBean.getData().getStatus_shooling();
            job_experience = jobDetailsBean.getData().getStatus_experience();
            if (jobDetailsBean.getData().getStatus_shooling() == 1) {
                tv_part_details_education_sure.setText("符合");
                tv_part_details_education_sure.setVisibility(View.VISIBLE);
                tv_part_details_education_sure.setTextColor(getResources().getColor(R.color
                        .cal_color_green));
            } else if (jobDetailsBean.getData().getStatus_shooling() == 2) {
                tv_part_details_education_sure.setText("不符合");
                tv_part_details_education_sure.setVisibility(View.VISIBLE);
                tv_part_details_education_sure.setTextColor(getResources().getColor(R.color.redColor));
            } else {
                tv_part_details_education_sure.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(jobDetailsBean.getData().getName_experience())) {
                tv_part_details_suffer.setVisibility(View.GONE);
            } else {
                tv_part_details_suffer.setVisibility(View.VISIBLE);
                tv_part_details_suffer.setText(jobDetailsBean.getData().getName_experience());
            }
            if (jobDetailsBean.getData().getStatus_experience() == 1) {
                tv_part_details_suffer_sure.setText("符合");
                tv_part_details_suffer_sure.setVisibility(View.VISIBLE);
                tv_part_details_suffer_sure.setTextColor(getResources().getColor(R.color
                        .cal_color_green));
            } else if (jobDetailsBean.getData().getStatus_experience() == 2) {
                tv_part_details_suffer_sure.setText("不符合");
                tv_part_details_suffer_sure.setVisibility(View.VISIBLE);
                tv_part_details_suffer_sure.setTextColor(getResources().getColor(R.color.redColor));
            } else {
                tv_part_details_suffer_sure.setVisibility(View.GONE);
            }
            apply_status = jobDetailsBean.getData().getApply_status();
            L.e("==========apply_status==========", apply_status);
            if ("0".equals(jobDetailsBean.getData().getApply_status())) {
                tv_part_job_details_apply.setText("申请面试");
                tv_part_job_details_apply.setBackgroundResource(R.drawable.shape_yellow_bottom);
            } else if ("1".equals(jobDetailsBean.getData().getApply_status())) {
                tv_part_job_details_apply.setText("已申请");
                tv_part_job_details_apply.setBackgroundResource(R.drawable.shape_gray_bottom);
            }
            u_id = jobDetailsBean.getData().getU_id();
            return;
        }
        //        if (requestUrl.contains("collectJob")) {
        //            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        //            initHttp();
        //            return;
        //        }
        if (requestUrl.contains("applyJob")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            showRightTip(map.get("message"));
            startActivity(ApplySureAty.class, null);
            return;
        }
    }
}
